//
//  MyTabbarCobntroller.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/7/23.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class MyTabbarCobntroller: UITabBarController ,MyCustomTabbarDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let tabbar = MyCustomTabbar()
        tabbar.dele = self
        self.delegate = self
        self.setValue(tabbar, forKey: "tabBar")
        
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue:"needLogin"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(needLoginMethod(notification:)), name: NSNotification.Name(rawValue:"needLogin"), object: nil)
    }
    
    @objc func needLoginMethod(notification: Notification){
        guard let userInfo = notification.object else {
            return
        }
        Utils.removeAllUserInfo()
        PKhud.showText(text: (userInfo as! postParameters)["notice"] as! String)
        Utils.goLoginCtrol(ctrl: UIViewController.currentViewController()!, isHidenBottomBar: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func centerButtonClick(tabbar: MyCustomTabbar) {
        let ctrl = UIViewController.currentViewController()
        if Utils.isLogined() {
            
            let vc = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "CommonListViewController") as! CommonListViewController
            vc.hidesBottomBarWhenPushed = true
            ctrl?.navigationController?.pushViewController(vc, animated: true)
        }else{
            if (UIApplication.shared.delegate as! AppDelegate).isShowLogin == false {
                (UIApplication.shared.delegate as! AppDelegate).isShowLogin = true
                PKhud.showText(text: "请先登录~")
                let loginCtrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "LoginViewController") as! LoginViewController
                ctrl?.present(UINavigationController.init(rootViewController: loginCtrl), animated: true, completion: {
                })
            }
        }
    }
}



extension UIViewController : UITabBarControllerDelegate {
    class func currentViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return currentViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            return currentViewController(base: tab.selectedViewController)
        }
        if let presented = base?.presentedViewController {
            return currentViewController(base: presented)
        }
        return base
    }
    
    public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let nav = viewController as! UINavigationController
        if nav.viewControllers.first is ShoppingCartViewController || nav.viewControllers.first is UserCenterViewController{
            guard !Utils.isLogined() else{
                return true
            }
            let ctrl = UIViewController.currentViewController()
            PKhud.showText(text: "请先登录~")
            Utils.goLoginCtrol(ctrl: ctrl!, isHidenBottomBar: false)
            return false
        }
        return true
    }
}
